/**
 * 
 */
package com.zyj.blog.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zyj.blog.common.SysConstants;
import com.zyj.blog.mapper.SysMenuMapper;
import com.zyj.blog.page.MybatisPageHelper;
import com.zyj.blog.page.PageRequest;
import com.zyj.blog.page.PageResult;
import com.zyj.blog.pojo.SysMenu;
import com.zyj.blog.pojo.SysMenuExample;
import com.zyj.blog.service.SysMenuService;

/**
 * @author zyj
 *
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
	
	@Autowired
	private SysMenuMapper sysMenuMapper;

	/* 
	 * 
	 */
	@Override
	public List<SysMenu> findTree(String username, int menuType) {
		List<SysMenu> sysMenus = new ArrayList<>();
		List<SysMenu> menus = new ArrayList<>();
		if(username == null || "".equals(username) || SysConstants.ADMIN.equalsIgnoreCase(username)) {
			SysMenuExample example = new SysMenuExample();
			menus = sysMenuMapper.selectByExample(example);
		}else {
			menus= sysMenuMapper.findByUserName(username);
		}
		for (SysMenu menu : menus) {
			if (menu.getParentId() == null || menu.getParentId() == 0) {
				menu.setLevel(0);
				sysMenus.add(menu);
			}
		}
		sysMenus.sort((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
		findChildren(sysMenus, menus, menuType);
		return sysMenus;
	}

	/**
	 * @param sysMenus
	 * @param menus
	 * @param menuType
	 */
	private void findChildren(List<SysMenu> sysMenus, List<SysMenu> menus, int menuType) {
		for (SysMenu SysMenu : sysMenus) {
			List<SysMenu> children = new ArrayList<>();
			for (SysMenu menu : menus) {
				if(menuType == 1 && menu.getType() == 2) {
					// 如果是获取类型不需要按钮，且菜单类型是按钮的，直接过滤掉
					continue ;
				}
				if (SysMenu.getId() != null && SysMenu.getId().equals(menu.getParentId())) {
					menu.setParentName(SysMenu.getName());
					menu.setLevel(SysMenu.getLevel() + 1);
					children.add(menu);
				}
			}
			SysMenu.setChildren(children);
			children.sort((o1, o2) -> o1.getOrderNum().compareTo(o2.getOrderNum()));
			findChildren(children, menus, menuType);
		}
	}

	/* 
	 * 
	 */
	@Override
	public List<SysMenu> findByUser(String username) {
		List<SysMenu> menus = new ArrayList<>();
		if(username == null || "".equals(username) || SysConstants.ADMIN.equalsIgnoreCase(username)) {
			SysMenuExample example = new SysMenuExample();
			menus = sysMenuMapper.selectByExample(example);
		}else {
			menus = sysMenuMapper.findByUserName(username);
		}
		return menus;
	}

	/* 
	 * 
	 */
	@Override
	public int save(SysMenu record) {
		if(record.getId() == null || record.getId() == 0) {
			return sysMenuMapper.insertSelective(record);
		}
		if(record.getParentId() == null) {
			record.setParentId(0L);
		}
		return sysMenuMapper.updateByPrimaryKeySelective(record);
	}

	/* (non-Javadoc)
	 * @see com.zyj.blog.service.CurdService#delete(java.lang.Object)
	 */
	@Override
	public int delete(SysMenu record) {
		return sysMenuMapper.deleteByPrimaryKey(record.getId());
	}

	/* (non-Javadoc)
	 * @see com.zyj.blog.service.CurdService#delete(java.util.List)
	 */
	@Override
	public int delete(List<SysMenu> records) {
		for(SysMenu record:records) {
			delete(record);
		}
		return 1;
	}

	/* (non-Javadoc)
	 * @see com.zyj.blog.service.CurdService#findById(java.lang.Long)
	 */
	@Override
	public SysMenu findById(Long id) {
		return sysMenuMapper.selectByPrimaryKey(id);
	}

	/* (non-Javadoc)
	 * @see com.zyj.blog.service.CurdService#findPage(com.zyj.blog.page.PageRequest)
	 */
	@Override
	public PageResult findPage(PageRequest pageRequest) {
		return MybatisPageHelper.findPage(pageRequest, sysMenuMapper);
	}

}
